var e = require;
var t = module;
var n = exports;
var o,
    i =
        (this && this.__extends) ||
        ((o = function (e, t) {
            return (o =
                Object.setPrototypeOf ||
                ({__proto__: []} instanceof Array &&
                    function (e, t) {
                        e.__proto__ = t;
                    }) ||
                function (e, t) {
                    for (var n in t) Object.prototype.hasOwnProperty.call(t, n) && (e[n] = t[n]);
                })(e, t);
        }),
        function (e, t) {
            function n() {
                this.constructor = e;
            }
            o(e, t), (e.prototype = null === t ? Object.create(t) : ((n.prototype = t.prototype), new n()));
        }),
    r =
        (this && this.__decorate) ||
        function (e, t, n, o) {
            var i,
                r = arguments.length,
                a = r < 3 ? t : null === o ? (o = Object.getOwnPropertyDescriptor(t, n)) : o;
            if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) a = Reflect.decorate(e, t, n, o);
            else
                for (var s = e.length - 1; 0 <= s; s--)
                    (i = e[s]) && (a = (r < 3 ? i(a) : 3 < r ? i(t, n, a) : i(t, n)) || a);
            return 3 < r && a && Object.defineProperty(t, n, a), a;
        },
    c =
        (this && this.__awaiter) ||
        function (e, a, s, c) {
            return new (s = s || Promise)(function (n, t) {
                function o(e) {
                    try {
                        r(c.next(e));
                    } catch (e) {
                        t(e);
                    }
                }
                function i(e) {
                    try {
                        r(c.throw(e));
                    } catch (e) {
                        t(e);
                    }
                }
                function r(e) {
                    var t;
                    e.done
                        ? n(e.value)
                        : ((t = e.value) instanceof s
                              ? t
                              : new s(function (e) {
                                    e(t);
                                })
                          ).then(o, i);
                }
                r((c = c.apply(e, a || [])).next());
            });
        },
    l =
        (this && this.__generator) ||
        function (n, o) {
            var i,
                r,
                a,
                s = {
                    label: 0,
                    sent: function () {
                        if (1 & a[0]) throw a[1];
                        return a[1];
                    },
                    trys: [],
                    ops: []
                },
                e = {next: t(0), throw: t(1), return: t(2)};
            return (
                "function" == typeof Symbol &&
                    (e[Symbol.iterator] = function () {
                        return this;
                    }),
                e
            );
            function t(t) {
                return function (e) {
                    return (function (t) {
                        if (i) throw new TypeError("Generator is already executing.");
                        for (; s; )
                            try {
                                if (
                                    ((i = 1),
                                    r &&
                                        (a =
                                            2 & t[0]
                                                ? r.return
                                                : t[0]
                                                ? r.throw || ((a = r.return) && a.call(r), 0)
                                                : r.next) &&
                                        !(a = a.call(r, t[1])).done)
                                )
                                    return a;
                                switch (((r = 0), (t = a ? [2 & t[0], a.value] : t)[0])) {
                                    case 0:
                                    case 1:
                                        a = t;
                                        break;
                                    case 4:
                                        return s.label++, {value: t[1], done: !1};
                                    case 5:
                                        s.label++, (r = t[1]), (t = [0]);
                                        continue;
                                    case 7:
                                        (t = s.ops.pop()), s.trys.pop();
                                        continue;
                                    default:
                                        if (
                                            !(a = 0 < (a = s.trys).length && a[a.length - 1]) &&
                                            (6 === t[0] || 2 === t[0])
                                        ) {
                                            s = 0;
                                            continue;
                                        }
                                        if (3 === t[0] && (!a || (t[1] > a[0] && t[1] < a[3]))) {
                                            s.label = t[1];
                                            break;
                                        }
                                        if (6 === t[0] && s.label < a[1]) {
                                            (s.label = a[1]), (a = t);
                                            break;
                                        }
                                        if (a && s.label < a[2]) {
                                            (s.label = a[2]), s.ops.push(t);
                                            break;
                                        }
                                        a[2] && s.ops.pop(), s.trys.pop();
                                        continue;
                                }
                                t = o.call(n, s);
                            } catch (e) {
                                (t = [6, e]), (r = 0);
                            } finally {
                                i = a = 0;
                            }
                        if (5 & t[0]) throw t[1];
                        return {value: t[0] ? t[1] : void 0, done: !0};
                    })([t, e]);
                };
            }
        };
Object.defineProperty(n, "__esModule", {value: !0}), (n.PanelManager = n.PanelShowResult = void 0);
var u,
    p = e("events"),
    h = e("base-panel"),
    a = cc._decorator,
    s = a.ccclass,
    d = a.property,
    t = a.menu,
    e = a.disallowMultiple;
((a = u = n.PanelShowResult || (n.PanelShowResult = {}))[(a.DONE = 0)] = "DONE"),
    (a[(a.FAILED = 1)] = "FAILED"),
    (a[(a.WAITING = 2)] = "WAITING");
var f,
    m,
    e =
        ((f = cc.Component),
        i(y, f),
        (m = y),
        Object.defineProperty(y, "ins", {
            get: function () {
                var e = cc.director.getScene().getChildByName("Canvas");
                return e.getComponent(m) || e.getComponentInChildren(m);
            },
            enumerable: !1,
            configurable: !0
        }),
        (y.prototype.onLoad = function () {
            (this.panelQueue = []), (this.panelList = []), (this.mask.active = !1);
        }),
        (y.prototype.open = function (r, a, s) {
            return c(this, void 0, Promise, function () {
                var t,
                    n,
                    o,
                    i = this;
                return l(this, function (e) {
                    switch (e.label) {
                        case 0:
                            return (s = null != s ? s : {}).inQueue && 0 < this.panelList.length
                                ? (this.panelQueue.push({prefab: r, data: a, opt: s}), [2, {panelResult: u.WAITING}])
                                : [
                                      4,
                                      new Promise(function (n, o) {
                                          cc.resources.load(r, cc.Prefab, function (e, t) {
                                              e ? o(e) : n(t);
                                          });
                                      })
                                  ];
                        case 1:
                            return (
                                (o = e.sent()),
                                cc.isValid(o)
                                    ? ((t = cc.instantiate(o)),
                                      (n = t.getComponent(h.default))
                                          ? ((o = t.getComponent(cc.Widget)) ||
                                                (((o = t.addComponent(cc.Widget)).verticalCenter = 0),
                                                (o.horizontalCenter = 0),
                                                (o.isAlignVerticalCenter = !0),
                                                (o.isAlignHorizontalCenter = !0)),
                                            (this.currentPanel = t),
                                            this.panelList.push({prefab: r, data: a, opt: s, node: t}),
                                            t.setParent(this.container),
                                            n.show(a),
                                            this.ensureMask(),
                                            p.layerEvent.emit({comp: n}),
                                            [
                                                2,
                                                new Promise(function (o) {
                                                    n.setFinishCallback(function (e, t) {
                                                        (i.currentPanel = null), (e.parent = null);
                                                        for (var n = 0; n < i.panelList.length; n++)
                                                            if (e == i.panelList[n].node) {
                                                                i.panelList.splice(n, 1);
                                                                break;
                                                            }
                                                        o({panelResult: u.DONE, values: t}),
                                                            i.onNext(),
                                                            i.scheduleOnce(function () {
                                                                i.ensureMask(), p.layerEvent.emit({comp: null});
                                                            }, 0);
                                                    });
                                                })
                                            ])
                                          : (console.warn("failed to load component of prefab", r),
                                            [2, {panelResult: u.FAILED}]))
                                    : (console.warn("invalid prefab", r), [2, {panelResult: u.FAILED}])
                            );
                    }
                });
            });
        }),
        (y.prototype.getPanelByPath = function (e) {
            for (var t = this.panelList.length - 1; 0 <= t; --t)
                if (this.panelList[t].prefab === e && null != this.panelList[t].node) {
                    var n = this.panelList[t].node.getComponent(h.default);
                    if (null != n) return n;
                }
            return null;
        }),
        (y.prototype.close = function (e) {
            for (var t, n = this.panelList.length - 1; 0 <= n; --n)
                if (this.panelList[n].prefab === e) {
                    null == this.panelList[n].node ||
                        (null != (t = this.panelList[n].node.getComponent(h.default)) && t.hide());
                    break;
                }
        }),
        (y.prototype.closeAllPanel = function () {
            this.panelQueue = [];
            for (var e, t = this.panelList.length - 1; 0 <= t; --t)
                null == this.panelList[t].node ||
                    (null != (e = this.panelList[t].node.getComponent(h.default)) && e.hide());
        }),
        (y.prototype.onNext = function () {
            var e;
            0 < this.panelList.length ||
                null != this.currentPanel ||
                0 == this.panelQueue.length ||
                ((e = this.panelQueue.shift()), this.open(e.prefab, e.data, e.opt));
        }),
        (y.prototype.ensureMask = function () {
            var e = this.panelList,
                t = e.length,
                n = this.container.childrenCount,
                o = this.mask;
            t <= 0
                ? (o.active = !1)
                : ((t = e[t - 1].node), (o.active = !0), (t.active = !0), o.setSiblingIndex(n), t.setSiblingIndex(n));
        }),
        (y.prototype.onMaskClick = function () {
            var e = this.panelList,
                t = e.length;
            t <= 0 || ((t = (e = e[t - 1]).opt), (e = e.node), t.clickMaskToClose && e.getComponent(h.default).hide());
        }),
        r([d(cc.Node)], y.prototype, "container", void 0),
        r([d(cc.Node)], y.prototype, "mask", void 0),
        (m = r([s, t("manager/panel-manager"), e], y)));
function y() {
    var e = (null !== f && f.apply(this, arguments)) || this;
    return (e.container = null), (e.mask = null), e;
}
n.PanelManager = e;
